<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\template\container\xhtml\instructions;

interface IInclude
{
}

class ContainerInclude extends Concrete implements IInclude, IConcrete
{
  const ROOT_COMMON = 'common';

  public function start()
  {
    $oTrigger = new \crcl\base\template\Trigger();
    $oTrigger->setScope(\crcl\base\template\container\xhtml\Instruction::SCOPE_CURRENT);
    $oTrigger->setInstruction($this);

    return $oTrigger;
  }

  public function executeCurrent()
  {
    $sFile = $this->getAttribute('include');
    $sFile = $this->filter($sFile);

    $sRoot = $this->getAttributeFalse('root');

    $aValues = array();
    foreach ($this->aAttributes as $sAttribute => $value)
    {
      if (\strpos($sAttribute, 'define:') === 0)
      {
        if (\strlen($sAttribute) == 7)
        {
          throw new Exception('Missing defined var',
            Exception::ERROR_INCLUDE_MISSINGDEFINEDVAR,
            array('sAttribute' => $sAttribute));
        }
        $sContent = \substr($sAttribute, 7); //define: weg
        $aValues[$sContent] = $value;
      }
    }

    $oCompiler = \crcl\base\template\Compiler::getInstance(\crcl\base\template\Compiler::TYPE_XHTML);
    $oCompiler->setContent($this->getFileContent($sFile, $sRoot));
    $this->sContent = $oCompiler->compileInclude($aValues);
  }

  public function verify()
  {
    $sFile = $this->getAttribute('include');
    if (\strpos($sFile, '..') !== \FALSE)
    {
      throw new Exception('Illegal path',
        Exception::ERROR_INCLUDE_ILLEGALPATH, array('sFile' => $sFile));
    }

    $bReturn = parent::verify();
    return $bReturn;
  }

  /** PRIVATE *************************************************************************************/

  protected $aAllowedAttributes = array('include', 'define', 'root');

  private function filter($sFile)
  {
    if (strpos($sFile, '.') === FALSE)
    {
      $sFile .= '.xhtml';
    }
    return $sFile;
  }

  private function getFileContent($sFile, $sRoot = \FALSE)
  {
    if (strtolower($sRoot) == self::ROOT_COMMON)
    {
      $sPath = \crcl\base\application\Path::DIR_COMMON_TEMPLATES;
    }
    else
    {
      $sPath = \crcl\base\application\Path::DIR_TEMPLATES;
    }
    $sFileLoad = \crcl\base\application\Path::getDirectory($sPath);
    if (\crcl\base\Mvc::hasTheme())
    {
      $sFileLoad .= \crcl\base\Mvc::getTheme() . '/';
    }
    $sFileLoad .= $sFile;
    $content = @\file_get_contents($sFileLoad);
    if ($content === \FALSE)
    {
      if (\crcl\base\Mvc::hasTheme())
      {
        $sFileLoad = \crcl\base\application\Path::getDirectory($sPath);
        $sFileLoad .= 'default/';
        $sFileLoad .= $sFile;
        $content = @\file_get_contents($sFileLoad);
        if ($content === \FALSE)
        {
          throw new Exception(
              'Missing file',
              Exception::ERROR_MISSINGFILE,
              array('file' => $sFileLoad, 'template' => $sFile));
        }
      }
      else
      {
        throw new Exception(
            'Missing file',
            Exception::ERROR_MISSINGFILE,
            array('file' => $sFileLoad, 'template' => $sFile));
      }
    }
    return $content;
  }
}